package com.software.loginregister.fragment.mineFrgment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.software.loginregister.R;
import com.software.loginregister.adapter.FollowAdapter;
import com.software.loginregister.entity.Result;
import com.software.loginregister.entity.vo.UserFollowDTO;
import com.software.loginregister.util.RetrofitClient;
import com.software.loginregister.util.SharedPreferencesUtil;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FollowListFragment extends Fragment {
    private RecyclerView recyclerView;
    private FollowAdapter adapter;
    private boolean isFollowers;

    public static FollowListFragment newInstance(boolean isFollowers) {
        FollowListFragment fragment = new FollowListFragment();
        Bundle args = new Bundle();
        args.putBoolean("isFollowers", isFollowers);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_follow_list, container, false);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        adapter = new FollowAdapter();
        recyclerView.setAdapter(adapter);

        isFollowers = getArguments().getBoolean("isFollowers");
        loadData();
    }

    private void loadData() {
        // 获取用户ID
        Context context = getContext();
        if (context == null) {
            Toast.makeText(requireContext(), "获取Context失败", Toast.LENGTH_SHORT).show();
            return;
        }
        Long userId = SharedPreferencesUtil.getUserId(context);
        if (isFollowers) {
            RetrofitClient.getInstance().getUserRelationApi()
                .getFollowers(userId)
                .enqueue(new Callback<Result<List<UserFollowDTO>>>() {
                    @Override
                    public void onResponse(Call<Result<List<UserFollowDTO>>> call, Response<Result<List<UserFollowDTO>>> response) {
                        if (response.isSuccessful() && response.body() != null && response.body().getCode() == 1) {
                            adapter.setData(response.body().getData());
                        }
                    }

                    @Override
                    public void onFailure(Call<Result<List<UserFollowDTO>>> call, Throwable t) {
                        Toast.makeText(getContext(), "加载失败: " + t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
        } else {
            // 加载关注列表
            RetrofitClient.getInstance().getUserRelationApi()
                .getFollowing(userId)
                .enqueue(new Callback<Result<List<UserFollowDTO>>>() {
                    @Override
                    public void onResponse(Call<Result<List<UserFollowDTO>>> call, Response<Result<List<UserFollowDTO>>> response) {
                        if (response.isSuccessful() && response.body() != null && response.body().getCode() == 1) {
                            adapter.setData(response.body().getData());
                        }
                    }

                    @Override
                    public void onFailure(Call<Result<List<UserFollowDTO>>> call, Throwable t) {
                        Toast.makeText(getContext(), "加载失败: " + t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
        }
    }
}
